package com.sskj.mine;

import android.text.TextUtils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.util.APKVersionCodeUtils;
import com.sskj.mine.bean.ImgHighBean;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.BasePresenter;
import com.sskj.mine.MineFragment;

import java.io.File;


/**
 * @author Hey
 * Create at  2019/05/01
 */
class MinePresenter extends BasePresenter<MineFragment> {
    public void requestUserInfo() {
        if (TextUtils.isEmpty(SPUtil.get(SPConfig.ACCOUNT, ""))) {
            return;
        }
        OkGo.<HttpData<UserData>>get(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .cacheKey(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(new JsonCallBack<HttpData<UserData>>(this, false) {
                    @Override
                    public void onSuccess(Response<HttpData<UserData>> response) {
                        HttpData<UserData> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateUserTitle(httpData.getData());
                        }
                    }
                });
    }

    // 跟新头像
    public void submitVerify(File file) {
        OkGo.<HttpData<ImgHighBean>>post(HttpConfig.BASE_URL + HttpConfig.HIGH_VERIFY_IMG)
                .isMultipart(false)
                .params("file_pic", file)
                .execute(new JsonCallBack<HttpData<ImgHighBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<ImgHighBean>> response) {
                        HttpData body = response.body();
                        if (body.status == HttpConfig.OK) {
                            ToastUtil.showShort(body.msg);
                            ImgHighBean hignBean = (ImgHighBean) body.getData();
                            chooseImg(((ImgHighBean) body.getData()).getUrl());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<ImgHighBean>> response) {
                        super.onError(response);
                        HttpData body = response.body();
                        mView.uploadFail();
                        ToastUtil.showShort(body.msg);
                    }
                });
    }


    public void chooseImg(String fileUrl) {
        OkGo.<HttpData<Object>>post(HttpConfig.BASE_URL + HttpConfig.CHOOSE_IMG)
                .isMultipart(false)
                .params("upic", fileUrl)
                .execute(new JsonCallBack<HttpData<Object>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<Object>> response) {
                        HttpData body = response.body();
                        if (body.status == HttpConfig.OK) {
                            ToastUtil.showShort(body.msg);
                        }
                    }
                });
    }

    // quxiao
    public void cancelShop() {
        OkGo.<HttpData<Object>>get(HttpConfig.BASE_URL + HttpConfig.SHOPCANCEL)
                .execute(new JsonCallBack<HttpData<Object>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<Object>> response) {
                        HttpData<Object> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            ToastUtil.showShort(httpData.getMsg());
                            mView.cancelShopSuccess();
                            requestUserInfo();
                        }
                    }
                });
    }


}
